package com.sljy.hzk.ui;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sljy.hzk.R;
import com.sljy.hzk.annotation.ActivityInfo;
import com.sljy.hzk.base.BaseActivity;
import com.sljy.hzk.base.UserInfo;
import com.sljy.hzk.entity.UserEntity;
import com.sljy.hzk.net.CommonResult;
import com.sljy.hzk.net.HttpHeper;
import com.sljy.hzk.net.service.UserServive;
import com.sljy.hzk.room.AppDataBase;
import com.sljy.hzk.room.dao.UserDao;
import com.sljy.hzk.util.NetWorkUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@ActivityInfo(layout = R.layout.activity_fix_passworld, title = "修改密码",isShowActionBar = true)
public class FixPswActivity extends BaseActivity {
    @BindView(R.id.old_psw)
    EditText old_pswText;
    @BindView(R.id.new_psw1)
    EditText new_psw_text_1;
    @BindView(R.id.new_psw2)
    EditText new_psw_text_2;
    @BindView(R.id.new_psw1_tips)
    TextView new_psw1_tips;
    @BindView(R.id.new_psw2_tips)
    TextView new_psw2_tips;
    @BindView(R.id.sure_btn)
    Button sure_btn;
    @BindView(R.id.show_psw_checkBox)
    CheckBox checkBox;
    UserEntity userInfo;
    UserDao userDao;
    private final String tips_content1 = "*密码长度不能小于6位字符";
    private final String tips_content2 = "*上下密码不一致";
    private final String tips_content3 = "修改失败";
    private final int PswNum = 6;

    @Override
    protected void init() {
        userInfo = UserInfo.getUserInfo();
        userDao = AppDataBase.getInstance().getUserDao();
        new_psw_text_1.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus)TipsPsw(1,new_psw_text_1,tips_content1);
                else  new_psw1_tips.setVisibility(View.GONE);
            }
        });
        new_psw_text_2.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus)TipsPsw(2, new_psw_text_2,tips_content1);
                else  new_psw2_tips.setVisibility(View.GONE);
            }
        });

        sure_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //检查网络连接情况
                if(!NetState()){
                    return;
                }
                //密码长度必须大于6位
                if(new_psw_text_1.getText().toString().length() < PswNum){
                    showToast(tips_content1);
                    return;
                }
                //验证密码和重复密码是否一致
                if(new_psw_text_1.length() != new_psw_text_2.length()){
                    TipsPsw(2,new_psw_text_2, tips_content2);
                    return;
                }
                if(!new_psw_text_1.getText().toString().equals(new_psw_text_2.getText().toString())){
                    new_psw2_tips.setText(tips_content2);
                    new_psw2_tips.setVisibility(View.VISIBLE);
                    return;
                }

               // Log.i("4",old_pswText.getText().toString()+"--"+old_pswText.length()+"--"+old_pswText.getText().toString().length());
                //发送修改请求
                HttpHeper.get().create(UserServive.class)
                        .fixPsw(userInfo.getUserId(),old_pswText.getText().toString(), new_psw_text_2.getText().toString())
                        .enqueue( new Callback<CommonResult<String>>(){
                            @Override
                            public void onResponse(Call<CommonResult<String>> call, Response<CommonResult<String>> response) {
                                if(response.body().getCode().equals("200")){
                                    showToast(response.body().getMsg());
                                    //更新本地离线数据库
                                    UserEntity u = userDao.getUserToId(userInfo.getUserId());
                                    if(u != null){
                                       if(u.getUserId() == userInfo.getUserId() ){
                                           u.setPassword(new_psw_text_2.getText().toString());
                                           userDao.Update(u);
                                       }
                                    }
                                    finish();
                                }else
                                    showToast(response.body().getMsg());
                            }

                            @Override
                            public void onFailure(Call<CommonResult<String>> call, Throwable t) {

                            }
                        });
            }
        });

        checkBox.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                showPsw(old_pswText, checkBox.isChecked());
                showPsw(new_psw_text_1, checkBox.isChecked());
                showPsw(new_psw_text_2, checkBox.isChecked());
            }
        });
    }

    /**
     * 提示密码长度不对
     * */
    private void TipsPsw(int index, TextView text, String Content){
        Log.i("5", text.getText().toString() +"--"+text.length());
        int l = text.length();
        if( l < PswNum){
            switch (index){
                case 1:
                    new_psw1_tips.setText(Content);
                    new_psw1_tips.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    new_psw2_tips.setText(Content);
                    new_psw2_tips.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    /**
     * 显示隐藏密码
     * */
    private void showPsw(TextView view, boolean isShow){
        if(isShow)
            //如果选中，显示密码
            view.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        else
            //否则隐藏密码
            view.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

}
